package com.sy.controller;

import com.github.pagehelper.Page;
import com.sy.dto.Table;
import com.sy.dto.UserSession;
import com.sy.service.*;
import com.sy.utils.CommonOperations;
import com.sy.utils.RedisOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public abstract class BaseController implements CommonOperations {
    @Autowired(required = false)
    protected HttpServletResponse res;
    @Autowired
    protected HttpServletRequest req;
    @Autowired
    protected LoginService loginService;
    @Autowired
    protected AdminService adminService;
    @Autowired(required = false)
    protected StudentService studentService;
    @Autowired
    protected TeacherService teacherService;
    @Autowired
    protected HomeworkService homeworkService;
    @Autowired
    protected StudyFileService studyFileService;
    @Autowired
    protected RedisOperations redisOperations;

    @Autowired
    protected NewLeavenWordsService newLeavenWordsService;
    /**
     * (封装的方法)将传入的集合直接包装成layui数据表渲染的类型
     */
    public <T> Table getTable(Page<T> pageList) {
        Table table = new Table();
        table.setCount(pageList.getTotal());
        table.setData(pageList);
        return table;
    }

    /**
     * 获取当前session的用户
     */
    public UserSession getSessionUser() {
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        return session;
    }

    /**
     * 获取当前session的用户的id
     */
    public Integer getSessionUserId() {
        return getSessionUser().getId();
    }
}
